<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 微信 解析 数据 套装
 * @param $url
 * @param null $data
 * @return mixed
 */
function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}


/**
 * 获取 离发布后 多长时间
 * @param $time
 * @return string
 */
function ago($time){
    $nowTime = (time() - $time);
    if ($nowTime < (5 * 60)) {
        return '1分钟前';
    } else if ($nowTime < (10 * 60)) {
        return '5分钟前';
    } else if ($nowTime < (15 * 60)) {
        return '10分钟前';
    } else if ($nowTime < (30 * 60)) {
        return '15分钟前';
    } else if ($nowTime < (45 * 60)) {
        return '30分钟前';
    } else if ($nowTime < (60 * 60)) {
        return '45分钟前';
    } else if ($nowTime < (24 * 60 * 60)) {
        return get_format_size($nowTime,1,'小时前');
    } else if ($nowTime < (30 * 24 * 60 * 60)) {
        return get_format_size($nowTime,24,'几天前');
    } else if ($nowTime < (12 * 30 * 24 * 60 * 60)) {
        return get_format_size($nowTime,30 * 24,'几月前');
    } else if ($nowTime > (365 * 24 * 60 * 60)) {
        return get_format_size($nowTime,365 * 24,'几年前');
    }
}

/**
 * 递归获取准确时间
 * @param $size    //当前时间大小  当前时间减去   创建时间
 * @param $H       //运算   获取是年月日还是小时
 * @param $str      // 获取小时 或者 年月日
 * @param int $count  // 计数判断   几个小时 几个月 ... 日 、年
 * @return string     //返回参数 添加遍历
 */
function get_format_size($size,$H,$str,$count = 0){
    if (bccomp($size,($H * 60 * 60 )) > 0) {
        $size = bcsub($size,($H * 60 * 60 ),2);
        return get_format_size($size,$H,$str,$count + 1);
    } else {
        return $count . $str;
    }

}

/**
 * 随机生成 盐值
 * @param int $length
 * @return bool|string
 */
function get_salt($length = 6){
    $random = "qwertyuiopasdfghjklzxcvbnm0123456789";

    $random = str_shuffle($random);

    return $str = substr(md5($random),0,$length);
}

/**
 * 所有同父亲的数据抱团
 * @param array $data
 * @return array
 */
function get_child_id(array $data = []) {
    $tempArr = [];
    foreach ($data as $key => $value) {
        $tempArr[$value["f_id"]][] = $value;
    }
    return get_tree_data($tempArr);
}

/**
 * 无限极获取 树状数据
 * @param array $data
 * @param int $level
 * @return mixed
 */
function get_tree_data(array $data = [],$level = 0)
{
    foreach ($data[$level] as $key => $value) {
        if (array_key_exists($value["id"],$data)) {
            $data[$level][$key]["children"] = get_tree_data($data,$value["id"]);
        }
    }
    return $data[$level];
}